package cn.iocoder.yudao.module.trade.order.service.handler;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.module.api.pay.bill.PayBillApi;
import cn.iocoder.yudao.module.api.pay.bill.dto.PayBillCreateDTO;
import cn.iocoder.yudao.module.api.pay.bill.enums.BillCategoryEnum;
import cn.iocoder.yudao.module.api.pay.bill.enums.BillTypeEnum;
import cn.iocoder.yudao.module.api.product.sku.dto.ProductPropertyValueRespDTO;
import cn.iocoder.yudao.module.trade.order.dal.dataobject.TradeOrderDO;
import cn.iocoder.yudao.module.trade.order.dal.dataobject.TradeOrderItemDO;
import jakarta.annotation.Resource;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 零钱账单 {@link TradeOrderHandler} 实现类
 *
 * @author 山野羡民
 */
@Component
public class TradeOrderMoneyBillHandler implements TradeOrderHandler {
    @Resource
    @Lazy // 延迟加载，避免循环依赖
    private PayBillApi payBillApi;

    @Override
    public void afterPayOrder(TradeOrderDO order, List<TradeOrderItemDO> orderItems) {
        PayBillCreateDTO createDTO = new PayBillCreateDTO();
        createDTO.setUserId(order.getUserId());
        createDTO.setCategory(BillCategoryEnum.MONEY);
        createDTO.setType(BillTypeEnum.PAY_PRODUCT);
        createDTO.setNumber(1d * order.getPayPrice());
        createDTO.setMark("购买【%s】".formatted(CollUtil.join(orderItems.stream().map(TradeOrderItemDO::getSpuName).distinct().toList(), "#")));
        createDTO.setRelateId(order.getNo());
        createDTO.setExtendField("{\"orderId\":\"%s\"}".formatted(order.getId()));
        payBillApi.expendAsync(createDTO);
    }

    @Override
    public void afterCancelOrder(TradeOrderDO order, List<TradeOrderItemDO> orderItems) {
        // 售后的订单项，已经在 afterCancelOrderItem 回滚库存，所以这里不需要重复回滚
        orderItems = filterOrderItemListByNoneAfterSale(orderItems);
        if (CollUtil.isEmpty(orderItems)) {
            return;
        }
        PayBillCreateDTO createDTO = new PayBillCreateDTO();
        createDTO.setUserId(order.getUserId());
        createDTO.setCategory(BillCategoryEnum.MONEY);
        createDTO.setType(BillTypeEnum.PAY_PRODUCT_REFUND);
        createDTO.setNumber(1d * order.getRefundPrice());
        createDTO.setMark("购买的【%s】退款".formatted(CollUtil.join(orderItems.stream().map(TradeOrderItemDO::getSpuName).distinct().toList(), "#")));
        createDTO.setRelateId(order.getNo());
        createDTO.setExtendField("{\"orderId\":\"%s\"}".formatted(order.getId()));
        payBillApi.incomeAsync(createDTO);
    }

    @Override
    public void afterCancelOrderItem(TradeOrderDO order, TradeOrderItemDO orderItem) {
        PayBillCreateDTO createDTO = new PayBillCreateDTO();
        createDTO.setUserId(order.getUserId());
        createDTO.setCategory(BillCategoryEnum.MONEY);
        createDTO.setType(BillTypeEnum.PAY_PRODUCT_REFUND);
        createDTO.setNumber(1d * order.getRefundPrice());
        createDTO.setMark("购买的【%s %s】退款".formatted(orderItem.getSpuName(), CollUtil.join(orderItem.getProperties().stream().map(ProductPropertyValueRespDTO::getValueName).toList(), "#")));
        createDTO.setRelateId(order.getNo());
        createDTO.setExtendField("{\"orderId\":\"%s\",\"orderItemId\":\"%s\"}".formatted(order.getId(), orderItem.getId()));
        payBillApi.incomeAsync(createDTO);
    }

}
